package com.yanqu.road.server.cmd.server;

import com.yanqu.road.NettyMgr;
import com.yanqu.road.define.YanQuNettyDefine;
import com.yanqu.road.connect.YanQuConnection;
import com.yanqu.road.pb.server.LoadProto;
import com.yanqu.road.server.connect.CrossAgentLinkedControl;
import com.yanqu.road.connect.ServerConnectionGroup;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.C_REGISTER, desc = "注册服务器")
public class RegisterServerCmd implements Command {

	@Override
	public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
		LoadProto.LoadCrossMsg reqMsg = LoadProto.LoadCrossMsg.parseFrom(packet.toByteArray());
		long serverId = reqMsg.getServerId();
		int serverIndex = reqMsg.getServerIndex();
		int linkIndex = reqMsg.getLinkIndex();

		YanQuConnection connection = new YanQuConnection(ctx.channel(), true, 1024, linkIndex);
		CrossAgentLinkedControl.init(serverId, 1);
		ServerConnectionGroup group = CrossAgentLinkedControl.addAgentLinkedGroup(serverId, serverIndex, connection, YanQuNettyDefine.LINK_COUNT_MAX, linkIndex);
		ctx.channel().attr(NettyMgr.LINKED_CLIENT).set(group);
		execute(group);
	}

	public void execute(ServerConnectionGroup client) throws Exception {

	}

}
